package org.example.dao.imp;

import org.example.dao.inter.CourseDao;
import org.example.entitys.managers.Course;
import org.example.entitys.relationship.Unit;
import org.example.entitys.relationship.student_unit_learning;
import org.example.entitys.relationship.teaching_evaluation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * @Description
 * @Author 魏锴
 * @Date 2023/11/15
 */

@Repository
public class CourseDaoImp implements CourseDao {
    @Autowired
    JdbcTemplate jdbcTemplate;

    @Override
    public int insertCourse(Course course) {
        String sql = "insert into t_course(`course_name`,`created_time`,`status`," +
                "`activity_id`) values (?, ?, ?, ?)";
        return jdbcTemplate.update(sql,
                course.getCourse_name(),
                course.getCreated_time(),
                course.getStatus(),
                course.getActivity_id()
        );
    }

    @Override
    public int insertLearnUnit(Unit unit) {
        String sql = "insert into t_course_learn_unit(`unit_title`,`doc_path`,`video_path`," +
                "`unit_introduce`,`father_id`,`is_chpt`,`course_id`,`learning_score`) " +
                "values (?, ?, ?, ?, ?, ? ,? ,?)";
        return jdbcTemplate.update(sql,
                unit.getUnit_title(),
                unit.getDoc_path(),
                unit.getVideo_path(),
                unit.getUnit_introduce(),
                unit.getFather_id(),
                unit.getIs_chpt(),
                unit.getCourse_id(),
                unit.getLearning_score()
        );
    }

    @Override
    public int insertEvaluation(teaching_evaluation eva) {
        String sql = "insert into t_teaching_evaluation(`stu_in_class_id`, `grade`," +
                " `evaluation`, `evaulated_time`, `course_id`, `evaluator`)" +
                " values (?, ?, ?, ?, ?, ?)";
        return jdbcTemplate.update(sql,
                eva.getStu_in_class_id(),
                eva.getGrade(),
                eva.getEvaluation(),
                eva.getEvaluated_time(),
                eva.getCourse_id(),
                eva.getEvaluator()
        );
    }

    @Override
    public boolean isExitCourseById(int course_id) {
        String sql = "select count(*) from t_course where id = ? ";
        return jdbcTemplate.queryForObject(sql, Integer.class, course_id) > 0;
    }

    @Override
    public boolean isExitLearnUnitById(int unit_id) {
        String sql = "select count(*) from t_course_learn_unit where id = ? ";
        return jdbcTemplate.queryForObject(sql, Integer.class, unit_id) > 0;
    }

    @Override
    public Course queryCourseById(int id) {
        String sql = "select id, course_name, created_time, status, " +
                "activity_id, teacher_id, class_id from t_course where id = ? ";
        return jdbcTemplate.queryForObject(sql, this::courseTransform, id);
    }

    @Override
    public Unit queryUnitById(int unit_id) {
        String sql = "select `id`, `unit_title`, `doc_path`, `video_path`, " +
                "`unit_introduce`, `father_id`, `is_chpt`, `course_id`, `learning_score` " +
                "from t_course_learn_unit where id = ? ";
        return jdbcTemplate.queryForObject(sql, this::unitTransform, unit_id);
    }

    @Override
    public List<student_unit_learning> queryUnitRecordByUnit(int unit_id) {
        String sql = "select id, stu_in_class_id, unit_id, finished_status, " +
                "finished_time from t_teaching_student_learning where unit_id = ? ";
        return jdbcTemplate.query(sql, this::learningTransform, unit_id);
    }

    @Override
    public List<student_unit_learning> queryUnitRecordBySCI(int stu_in_class_id) {
        String sql = "select id, stu_in_class_id, unit_id, finished_status, " +
                "finished_time from t_teaching_student_learning where stu_in_class_id = ? ";
        return jdbcTemplate.query(sql, this::learningTransform, stu_in_class_id);
    }

    @Override
    public List<Course> queryAllCourse() {
        String sql = "select id, course_name, created_time, status, " +
                "activity_id, teacher_id, class_id from t_course ";
        return jdbcTemplate.query(sql, this::courseTransform);
    }

    @Override
    public List<Unit> queryAllUnit() {
        String sql = "select id, unit_title, doc_path, video_path, " +
                "unit_introduce, father_id, is_chpt, course_id, " +
                "learning_score from t_course_learn_unit ";
        return jdbcTemplate.query(sql, this::unitTransform);
    }

    @Override
    public Course courseTransform(ResultSet rs, int RowNum) throws SQLException {
        return new Course(
                rs.getInt("id"),
                rs.getString("course_name"),
                rs.getDate("created_time"),
                rs.getInt("status"),
                rs.getInt("activity_id"),
                rs.getInt("teacher_id"),
                rs.getInt("class_id")
        );
    }

    @Override
    public Unit unitTransform(ResultSet rs, int RowNum) throws SQLException {
        return new Unit(
                rs.getInt("id"),
                rs.getString("unit_title"),
                rs.getString("doc_path"),
                rs.getString("video_path"),
                rs.getString("unit_introduce"),
                rs.getInt("father_id"),
                rs.getInt("is_chpt"),
                rs.getInt("course_id"),
                rs.getInt("learning_score")
        );
    }

    public student_unit_learning learningTransform(ResultSet rs, int RowNum) throws SQLException {
        return new student_unit_learning(
                rs.getInt("id"),
                rs.getInt("stu_in_class_id"),
                rs.getInt("unit_id"),
                rs.getInt("finish_status"),
                rs.getDate("finish_time")
        );
    }


}
